package actions
{
	import flash.display.DisplayObject;
	import flash.events.Event;
	import flash.events.EventDispatcher;
	import flash.utils.getTimer;
	
	import interfaces.IAction;
	
	public class PropsAction extends EventDispatcher implements IAction
	{
		private var m_target:DisplayObject;
		
		private var m_start:Boolean;
		private var t:Number;
		private var m_speed:Number;
		private var m_acceleration:Number;
		private var m_props:Array;
		private var m_now:Number;
		private var m_pause:Boolean;
		private var m_pauseTime:Number;
		
		public function PropsAction(mc:DisplayObject)
		{
			m_target = mc;
			m_props = new Array();
		}
		
		public function set props(a:Array):void
		{
			m_props = m_props.concat(a);
			for(var i:int = 0; i < m_props.length; i++)
			{
				m_target[m_props[i].prop] = m_props[i].init;
			}
		}
		
		public function get target():DisplayObject
		{
			return m_target;
		}
		
		public function set start(b:Boolean):void
		{
			m_start = b;
			if(b)
			{
				t = 0;
				m_now = getTimer();
				m_pause = false;				
			}else
			{
				dispatchEvent(new Event(Event.COMPLETE));
			}
		}
		
		public function get start():Boolean
		{
			return m_start;
		}
		
		public function set pause(b:Boolean):void
		{
			m_pause = b;
			if(b)
			{
				m_pauseTime = getTimer();
			}else
			{
				m_now += (getTimer() - m_pauseTime);
			}
		}
		
		public function get pause():Boolean
		{
			return m_pause;
		}
		
		public function set speed(n:Number):void
		{
			if(n > 0)m_speed = n;
		}
		
		public function get speed():Number
		{
			return m_speed;
		}
		
		public function set acceleration(n:Number):void
		{
			m_acceleration = n;
		}
		
		public function get acceleration():Number
		{
			return m_acceleration;
		}
		
		public function update(e:Object = null):void
		{
			if(!start || pause)return;	
			
			if(t >= 1)
			{
				start = false;
				if(m_props.length > 0)
					m_target[m_props[i].prop] = m_props[i].end;
				return;
			}
			if(!m_acceleration)m_acceleration = 0;
			m_speed += m_acceleration * ( getTimer() - m_now );
			t += m_speed; 
			m_now = getTimer();
  
			if(m_props.length == 0)return;
			for(var i:int = 0; i < m_props.length; i++)
			{
				m_target[m_props[i].prop] = (m_props[i].end - m_props[i].init) * t + m_props[i].init ;
			}
		}
		
		public function clone():PropsAction
		{
			var pa:PropsAction = new PropsAction(m_target);
			pa.props = m_props.concat(m_props);
			pa.acceleration = acceleration;
			pa.speed = speed;
			return pa;
		}
	}
}